﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using Demo.Mmose.Core.Common;
using System.Collections.Generic;
#endregion

namespace Demo.Mmose.Core.Entity.Suit.Spell
{
    /// <summary>
    /// 技能
    /// </summary>
    public class Spell
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="spells"></param>
        public Spell()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="spells"></param>
        public Spell( long spellId )
        {
            m_Serial = spellId;
        }

        #endregion

        #region zh-CHS 共有属性 | en Public Properties

        #region zh-CHS Serial属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private Serial m_Serial = new Serial();
        #endregion
        /// <summary>
        /// 唯一序列号GUID
        /// </summary>
        public Serial Serial
        {
            get { return m_Serial; }
            set { m_Serial = value; }
        }

        #endregion

        #region zh-CHS Target属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private Unit m_Target = null;
        #endregion
        /// <summary>
        /// 施法者的对象
        /// </summary>
        public Unit Target
        {
            get
            {
                // 如果没有技能列表 直接返回自己
                SpellLine spellLine = m_SpellLine;
                if ( spellLine == null )
                    return m_Target;

                Spell firstSpell = spellLine.FirstSpell;
                if ( firstSpell == null )
                    return m_Target;

                // 如果自己不是第一个技能列表里面的技能就返回第一个技能的施法者的对象
                if ( firstSpell != this )
                    return firstSpell.Target;
                else
                    return m_Target;
            }
            set
            {
                // 如果没有技能列表 直接设置 然后返回
                SpellLine spellLine = m_SpellLine;
                if ( spellLine == null )
                {
                    m_Target = value;
                    return;
                }

                // 如果有技能列表 且第一个技能等于自己 则设置
                if ( spellLine.FirstSpell == this )
                {
                    m_Target = value;
                    return;
                }
            }
        }

        #endregion

        #region zh-CHS Caster属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private Unit m_Caster = null;
        #endregion
        /// <summary>
        /// 施法者
        /// </summary>
        public Unit Caster
        {
            get
            {
                // 如果没有技能列表 直接返回自己
                SpellLine spellLine = m_SpellLine;
                if ( spellLine == null )
                    return m_Caster;

                Spell firstSpell = spellLine.FirstSpell;
                if ( firstSpell == null )
                    return m_Caster;

                // 如果自己不是第一个技能列表里面的技能就返回第一个技能的施法者
                if ( firstSpell != this )
                    return firstSpell.Caster;
                else
                    return m_Caster;
            }
            set
            {
                // 如果没有技能列表 直接设置 然后返回
                SpellLine spellLine = m_SpellLine;
                if ( spellLine == null )
                {
                    m_Caster = value;
                    return;
                }

                // 如果有技能列表 且第一个技能等于自己 则设置
                if ( spellLine.FirstSpell == this )
                {
                    m_Caster = value;
                    return;
                }
            }
        }

        #endregion

        #region zh-CHS CastingTime属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 冷却咒语时间
        /// </summary>
        private TimeSpan m_CastingTime = TimeSpan.Zero;
        #endregion
        /// <summary>
        /// 释放咒语时间
        /// </summary>
        public TimeSpan CastingTime
        {
            get { return m_CastingTime; }
            set { m_CastingTime = value; }
        }
        #endregion

        #region zh-CHS CoolDownTime属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 冷却咒语时间
        /// </summary>
        private TimeSpan m_CoolDownTime = TimeSpan.Zero;
        #endregion
        /// <summary>
        /// 冷却咒语时间
        /// </summary>
        public TimeSpan CoolDownTime
        {
            get { return m_CoolDownTime; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="coolDownTime"></param>
        public void SetNextCoolDownTime( TimeSpan coolDownTime )
        {
            // 每次设置都产生新的有效冷却的咒语时间

            m_NextCoolDownTime = DateTime.Now + coolDownTime;
            m_CoolDownTime = coolDownTime;
        }

        #endregion

        #region zh-CHS NextCoolDownTime属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 冷却咒语时间
        /// </summary>
        private DateTime m_NextCoolDownTime = DateTime.MinValue;
        #endregion
        /// <summary>
        /// 下一次冷却咒语时间有效
        /// </summary>
        public DateTime NextCoolDownTime
        {
            get { return m_NextCoolDownTime; }
        }

        #endregion

        #region zh-CHS SpellTemplate属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SpellTemplate m_SpellTemplate = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public SpellTemplate SpellTemplate
        {
            get { return m_SpellTemplate; }
            set { m_SpellTemplate = value; }
        }

        #endregion

        #region zh-CHS SpellLine属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SpellLine m_SpellLine = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public SpellLine SpellLine
        {
            get { return m_SpellLine; }
            internal set
            {
                if ( m_SpellLine != null )
                    throw new Exception( "Spell.SpellLine(...) - m_SpellLine == null error!" );

                m_SpellLine = value;
            }
        }

        #endregion

        #region zh-CHS PreviousInSpellLine属性 | en Public Properties

        /// <summary>
        /// 
        /// </summary>
        public Spell PreviousInSpellLine
        {
            get
            {
                LinkedListNode<Spell> thisSpell = m_ThisSpellNode;
                if ( thisSpell == null )
                    return null;

                LinkedListNode<Spell> previousSpell = thisSpell.Previous;
                if ( previousSpell == null )
                    return null;

                return previousSpell.Value;
            }
        }

        #endregion

        #region zh-CHS NextInSpellLine属性 | en Public Properties

        /// <summary>
        /// 
        /// </summary>
        public Spell NextInSpellLine
        {
            get
            {
                LinkedListNode<Spell> thisSpell = m_ThisSpellNode;
                if ( thisSpell == null )
                    return null;

                LinkedListNode<Spell> nextSpell = thisSpell.Next;
                if ( nextSpell == null )
                    return null;

                return nextSpell.Value;
            }
        }

        #endregion

        #endregion

        #region zh-CHS 内部属性 | en Internal Properties

        #region zh-CHS ThisLinkedListNode属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private LinkedListNode<Spell> m_ThisSpellNode = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        internal LinkedListNode<Spell> ThisLinkedListNode
        {
            get { return m_ThisSpellNode; }
            set
            {
                if ( m_ThisSpellNode != null )
                    throw new Exception( "Spell.ThisLinkedListNode(...) - m_ThisSpell == null error!" );

                m_ThisSpellNode = value;
            }
        }

        #endregion

        #endregion

        #region zh-CHS 共有方法 | en Public Methods

        /// <summary>
        /// 
        /// </summary>
        public void ActionSpell( SpellHandlers spellHandlers, object arg )
        {
            SpellCast.StartCast( m_Serial, this, null );

            ISpellHandler spellHandler = spellHandlers.GetHandler( m_Serial );
            if ( spellHandler != null )
                spellHandler.Cast( this, arg );
        }

        /// <summary>
        /// 
        /// </summary>
        public void AsyncActionSpell( SpellHandlers spellHandlers, object arg )
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        public void ActionSpellLine( SpellHandlers spellHandlers, object arg )
        {
            this.ActionSpell( spellHandlers, arg );

            Spell nextSpell = this.NextInSpellLine;
            while ( nextSpell != null )
            {
                nextSpell.ActionSpell( spellHandlers, arg );
                nextSpell = nextSpell.NextInSpellLine;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void AsyncActionSpellLine( SpellHandlers spellHandlers, object arg )
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
#endregion